package com.kgc.sbt.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sbt.domain.*;
import com.kgc.sbt.mapper.UserMapper;
import com.kgc.sbt.api.UserApi;
import com.kgc.sbt.mapper.VipMapper;
import com.kgc.sbt.mapper.VipRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import java.math.BigDecimal;
import java.util.UUID;

/**
 * @Auther: Administrator
 * @Date: 2021年 06月22日 09:18
 * @Description: 用户业务实现类
 */
@Service
@Slf4j
public class UserProvider implements UserApi {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private VipMapper vipMapper;

    @Autowired
    private VipRecordMapper vipRecordMapper;

    @Override
    public User queryUser(String phoneNo) {
        return userMapper.selectByPhone(phoneNo);
    }

    @Override
    public User queryUserByOpenid(String openid) {
        return userMapper.selectByOpenid(openid);
    }

    @Override
    public void addUser(User user) {
        userMapper.insertSelective(user);
    }

    @Override
    public User queryUserByPwd(String phoneNo, String password) {
        // 创建多条件对象查询
        UserExample userExample = new UserExample();
        userExample.setDistinct(false);
        // 创建多个条件
        UserExample.Criteria criteria1 = userExample.createCriteria();
        criteria1.andPhoneEqualTo(phoneNo);
        criteria1.andPasswordEqualTo(password);
        return userMapper.selectByPhoneNoAndPwd(userExample);
    }

    @Override
    public boolean updateOpenId(String openid, String phone) {
        return userMapper.updateOpenidByPhone(openid, phone) > 0;
    }

    @Override
    public String chargeVipOfTenDays(String phone) {
        String sql = "select id from user where phone = " + phone;
        int userId = jdbcTemplate.queryForObject(sql, Integer.TYPE);
        String tradeNo = "V" + System.currentTimeMillis() + UUID.randomUUID().toString().replace("-", "").substring(0, 5);
        VipRecord vipRecord = VipRecord.builder().userId(userId).type(1).price(new BigDecimal("0.01")).tradeNo(tradeNo).build();
        vipRecordMapper.insertSelective(vipRecord);
        return tradeNo;
    }

    @Override
    public String chargeVipOfTwentyDays(String phone) {
        String sql = "select id from user where phone = " + phone;
        int userId = jdbcTemplate.queryForObject(sql, Integer.TYPE);
        String tradeNo = "V" + System.currentTimeMillis() + UUID.randomUUID().toString().replace("-", "").substring(0, 5);
        vipRecordMapper.insertSelective(VipRecord.builder().userId(userId).type(2).price(new BigDecimal("0.02")).tradeNo(tradeNo).build());
        return tradeNo;
    }

    @Override
    public Vip queryVip(String phoneNo) {
        String sql = "select id from user where phone = " + phoneNo;
        int userId = jdbcTemplate.queryForObject(sql, Integer.TYPE);
        return vipMapper.selectByUserId(userId);
    }

    @Override
    public void createCommonVip(String phoneNo) {
        String sql = "select id from user where phone = " + phoneNo;
        int userId = jdbcTemplate.queryForObject(sql, Integer.TYPE);
        vipMapper.insertSelective(Vip.builder().userId(userId).type(0).build());
    }

    @Override
    public void updateVip(String phoneNo) {
        String sql = "select id from user where phone = " + phoneNo;
        int userId = jdbcTemplate.queryForObject(sql, Integer.TYPE);
        vipMapper.updateVipByUserId(userId);
    }

    @Override
    public void addVipTenDays(String phoneNo) {
        String sql = "select id from user where phone = " + phoneNo;
        int userId = jdbcTemplate.queryForObject(sql, Integer.TYPE);
        vipMapper.addVipTenDaysByUserId(userId);
    }

    @Override
    public void delVipRecord(String tradeNo) {
        String sql = "select id from vip_record where trade_no = '" + tradeNo + "'";
        int id = jdbcTemplate.queryForObject(sql, Integer.TYPE);
        vipRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateWechatUnbind(String phoneNo) {
        userMapper.updateWechatUnbind(phoneNo);
    }

    @Override
    public boolean updateInfo(UserTT user) {

        UserTT usertt1 = userMapper.selectPhoneInUser(user.getPhone());

        log.info("------ 更新中");
        if (usertt1 == null) {
            // 没有用户信息
            userMapper.updateById(user);
            log.info("------ 更新成功");
            return true;

        }
        if (user.getId() == usertt1.getId()) {
            userMapper.updateById(user);
            log.info("------ 同一个用户更新手机号，更新成功");
            return true;
        } else {
            log.info("------ 刚号码已被使用过，请重新输入");
            return false;
        }
    }


    // =============================================================================================


    @Override
    public User selectInfoById(Integer id) {
        User user = userMapper.selectUserTTById(id);
        if (userMapper.selectUserTTById(id) == null) {
            return null;
        }
        return user;
    }
}
